package com.oscchina.xushao.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.oscchina.xushao.MainActivity;
import com.oscchina.xushao.R;
import com.oscchina.xushao.adapter.NewsFragmentPagerAdapter;
import com.oscchina.xushao.common.SimpleTwoTabsListener;

/**
 * 侧边栏导航-资讯
 */
public class InfoFragment extends Fragment {

    private ViewPager viewPager;
    private List<Fragment> mentionFragments = new ArrayList<Fragment>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initActionBarAndTab(getCompatActionBar(), R.string.tab_news_info, R.string.tab_news_blog, R.string.tab_news_read, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_layout, container, false);
        viewPager = (ViewPager) view;
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewPager.setOffscreenPageLimit(3);
        viewPager.setOnPageChangeListener(onPageChangeListener);

        NewsFragmentPagerAdapter adapter = new NewsFragmentPagerAdapter(this, viewPager,
                getChildFragmentManager(), (MainActivity) getActivity(), mentionFragments);
        viewPager.setAdapter(adapter);
    }

    private ActionBar getCompatActionBar() {
        return ((MainActivity) getActivity()).getSupportActionBar();
    }

    /**
     * 初始化actionbar并给viewpager不同页面设置标题
     *
     * @param actionBar
     * @param firstTab
     * @param secondTab
     * @param nav
     */
    public void initActionBarAndTab(ActionBar actionBar, int firstTab, int secondTab, int thirdTab, int nav) {
        //  actionBar.setLogo(R.drawable.slm_action_news);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.removeAllTabs();

        SimpleTwoTabsListener tabListener = new SimpleTwoTabsListener(viewPager);
        actionBar.addTab(actionBar.newTab().setText(firstTab).setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText(secondTab).setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText(thirdTab).setTabListener(tabListener));

        if (actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
            actionBar.setSelectedNavigationItem(nav);
        }
    }

    /**
     * ViewPager.SimpleOnPageChangeListener
     */
    ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (getCompatActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
                getCompatActionBar().setSelectedNavigationItem(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
            // 暂不知具体作用
            switch (state) {
                case ViewPager.SCROLL_STATE_SETTLING:
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 返回最新资讯Fragment-这里使用了嵌套的Fragment
     *
     * @return fragment InfoNewsFragment
     */
    public InfoNewsFragment getNewsChildFragment() {
        // 这里的处理有问题，测试发现都为null，不过只在初始时执行
        InfoNewsFragment fragment = ((InfoNewsFragment) getChildFragmentManager().findFragmentByTag(InfoNewsFragment.class.getName()));
        if (fragment == null) {
            fragment = new InfoNewsFragment();
        }
        return fragment;
    }

    /**
     * 返回最新博客、推荐阅读Fragment-这里使用了嵌套的Fragment
     *
     * @param type
     * @return fragment InfoBlogFragment
     */
    public InfoBlogFragment getNewsBlogFragment(String type) {
        InfoBlogFragment fragment = ((InfoBlogFragment) getChildFragmentManager().findFragmentByTag(InfoBlogFragment.class.getName()));
        if (fragment == null) {
            fragment = InfoBlogFragment.newInstance(type);
        }
        return fragment;
    }

}
